// pages/dayReportEdit/dayReportEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weeArray:['日','一','二','三','四','五','六'],
    dayArr:[],
    fileList:[],
    info:{}
  },

  /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'workReportDay',fileType:'日报'}).then((res)=>{
 // 上传完成需要更新 fileList
      const { fileList = [] } = that.data;
      fileList.push({ ...file, url: res.result.src,id:res.result.id });
      that.setData({ fileList });
    })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    console.log(index)
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
  
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'workReportDay'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },


  /**提交 */
  commit(e){
    let para = e.detail.value;
    let fileList = this.data.fileList;
    let fileId = [];
    fileList.forEach((v,i)=>{
      fileId.push(v.id)
    })
    para.fileId = JSON.stringify(fileId);
    para.id=api.isBlank(this.data.info.id)?'':this.data.info.id,
    para.location = this.data.info.now_date,
    para.type='日报';
    api.curlPost('?cla=workReportApp&fun=commitEdit',para).then((res)=>{
      wx.redirectTo({
        url: '../dayReportInfo/dayReportInfo?id='+res.data.id
      })
    })
  },

  /**选择日期 */
  chooseDate(e){
    this.requstData(e.currentTarget.dataset.date)
  },
  /**切换月份 */
  changeMonth(e){
    let type = e.currentTarget.dataset.type;
    let month = this.data.now_month;
    console.log(month)
    if(type=='pre'){
      month=api.getPreMonth(api.formatDateCRM('Y/m',month));
    }else if(type=='next'){
      month=api.getNextMonth(api.formatDateCRM('Y/m',month));
    }
    //请求数据
    this.requstData('',month)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   if(api.isBlank(options.id)){
      this.requstData()
   } else{
      this.requstData('','',options.id)
   }
  },

  requstData(date='',month='',id=''){
    api.curlPost('?cla=workReportApp&fun=detail',{type:'日报',id:id,date:date,month:month}).then((res)=>{
      this.setData({
        info:res.data.result,
        dayArr:res.data.result.dayRecord,
        now_month:res.data.result.now_month,
        now_date:res.data.result.now_date,
        now_month_text:api.formatDateCRM('Y年m月',res.data.result.now_month),
        fileList:res.data.result.attachFile?res.data.result.attachFile:[],
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})